/**
 * Project Name:credit-rest
 * File Name:GlobalExceptionHandlerAdvice.java
 * Package Name:com.huarong.credit.exception
 * Date:2019年12月9日下午7:40:10
 * Copyright (c) 2019, hejiahua@szhuarong.com All Rights Reserved.
 *
*/

package com.helper.domain.advice;

import org.springframework.core.MethodParameter;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import lombok.extern.slf4j.Slf4j;

@ControllerAdvice
@Slf4j
public class ResultResponseBodyAdvice implements ResponseBodyAdvice<Object> {
	/**
	 * @Title: 捕获500业务异常 @Description: 捕获业务异常 @param: @param
	 *         ex @param: @return @return: ResponseEntity<Object> @throws
	 */
	@ExceptionHandler(value = Exception.class)
	public ResponseEntity<Object> handleNoHandlerFoundException(Exception ex) {
		log.error("出现未知异常",ex);
		return new ResponseEntity<Object>(ex.getLocalizedMessage(), HttpStatus.OK);
	}

	@Override
	public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
		return false;
	}

	@Override
	public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
			Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request,
			ServerHttpResponse response) {
		return body;
	}
}
